from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from config.settings import config
from db.connection import engine, Base
from api.routes.conversation import router as conversation_router
from api.routes.scene import router as scene_router
from api.routes.asr import router as asr_router
from api.routes.nlu import router as nlu_router
from api.routes.tools import router as tools_router
from api.routes.knowledge import router as knowledge_router
from utils.image_path_manager import image_manager

# Create database tables
Base.metadata.create_all(bind=engine)

# Create FastAPI app
app = FastAPI(title="EmoBot API", version="1.0")

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, restrict this to specific origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static files directory for images using the image manager
app.mount("/images", StaticFiles(directory=image_manager.base_dir), name="images")

# Register API routes
app.include_router(conversation_router)
app.include_router(scene_router)
app.include_router(asr_router)
app.include_router(nlu_router)
app.include_router(tools_router)
app.include_router(knowledge_router)


# Root endpoint
@app.get("/")
def read_root():
    return {
        "message": "Welcome to EmoBot API",
        "version": "1.0",
        "endpoints": [
            "/api/conversation/start",
            "/api/conversation/interact",
            "/api/conversation/end",
            # "/api/conversation/register-face",  # 人脸识别功能已注释
            "/api/user-profiles/",
            "/api/user-profiles/{user_id}",
            # "/api/scene/get",  # 场景识别功能已注释
            "/api/asr/recognize",
            "/api/asr/recognize-bytes",
            "/api/nlu/analyze",
            "/api/nlu/analyze-json",
            "/api/tools/search",
            "/api/tools/weather",
            "/api/tools/analyze-need",
            "/api/tools/health",
            "/api/conversation/context/summary",
            # "/api/conversation/reflection/check",  # 反思模块已注释
        ],
    }


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host=config.HOST, port=config.PORT)
